<template>
  <div class="loginbox">
    <div class="login">
      <div>
        <img src="@/assets/login/login-bigimg.jpg" class="login-img" alt="" />
      </div>

      <el-form
        class="login-window"
        :label-position="'top'"
        label-width="100px"
        :model="formLabelAlign"
        style="max-width: 460rem"
      >
        <el-form-item>
          <div class="logText">管理员登录</div>
        </el-form-item>
        <el-form-item label="账户名"  >
          <el-input  v-model="formLabelAlign.name"   />
        </el-form-item>
        <el-form-item label="密码" >
          <el-input v-model="formLabelAlign.password" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit" >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>



  
  <script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { UserLogin } from '@/utils/lxz-axios'
import { useRouter } from 'vue-router'

const router = useRouter()

const labelPosition = ref<FormProps['labelPosition']>('right')

const formLabelAlign = reactive({
  name: '',
  password: '',
  type: '',
})

// 引入pinia数据
import { storeToRefs } from 'pinia'
import { useCounterStore } from '../../stores/Lxz.js'

// 获取pinia实例内容
const FilmDataStore = useCounterStore()

// 获取pinia数据使用这种方式
const { users } = storeToRefs(FilmDataStore)

// //引入pinia内的方法
const { setUsers } = FilmDataStore

const onSubmit = () => {
  UserLogin(formLabelAlign.name, formLabelAlign.password)
    .then((res) => {
      console.log('yes', res)
      if (res.data.logintype == 2 && res.data.usertype == 'admin') {
        ElMessage({
          message: `登录成功，欢迎您!管理员${res.data.usname}`,
          type: 'success',
        })

        localStorage.setItem('token', res.data.token)
        localStorage.setItem('users', res.data.usname)
        setUsers(res.data.usname)
        router.push('/')
      } else if (res.data.logintype == 2 && res.data.usertype != 'admin') {
        ElMessage({
          message: `登录失败！当前账户非管理员！`,
          type: 'warning',
        })
      } else {
        ElMessage.error(res.data.error)
      }
    })
    .catch((err) => {
      console.log('error', err)
    })
}
</script>
  
<style lang="less" scoped>
.loginbox {
  width: 100vw;
  height: 100vh;
  // background-color: bisque;
  display: flex;
  justify-content: center;
  align-items: center;
 
}
.login {
  // position: fixed;
  // top: 50%;
  // left: 50%;
  // transform: translate(-50%, -50%);
  background-color: white;
  display: flex;
  font-size: 0;

  box-shadow: 0px 0px 4px 0px rgba(94, 94, 94, 0.3);
  .login-img {
    height: 500rem;
  }

  .login-window {
    padding: 80rem 35rem;
    box-sizing: border-box;

    width: 25vw;
    height: 35vw;

    // display: flex;
    // flex-flow: column;
    // justify-content: center;

    .logText {
      font-size: 30rem;
      padding-bottom: 60rem;
    }
  }
}
</style>


